import os
from openai import OpenAI

class AliOpenAI:
    def __init__(self):
        self.api_key = 'sk-b68066e5513c4d438d9a1581fc655a53'
        self._ali_open_ai_client = OpenAI(
            api_key=self.api_key,
            base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
        )


    def chat(self, message:str):
        completion = self._ali_open_ai_client.chat.completions.create(
            # 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
            model="qwen-turbo",
            messages=[
                {'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': message}],
        )

        # print(completion.model_dump_json())
        answer = completion.choices[0].message.content
        print(answer)
        print(completion.usage.total_tokens)

        return answer



if __name__ == '__main__':
    ali_openai = AliOpenAI()
    ali_openai.chat('python 中如何进行线程池')
